package com.guli.mall.serviceBase.config;


import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.Session;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/*
 * @Author 罗
 * @date 2020/10/19 - 9:02 下午
 */
@Slf4j
@ConditionalOnClass(Session.class)
@ConditionalOnWebApplication
public class HttpSessionConfig implements BeanClassLoaderAware {

    private ClassLoader loader;

    /**
     * 原配置地址参考
     * https://github.com/spring-projects/spring-session/tree/2.3.1.RELEASE/spring-session-samples/spring-session-sample-boot-redis-json/src/main/java/sample/config
     * 配置 spring session 的序列化器
     * 方法名或者 @Bean() 必须是 springSessionDefaultRedisSerializer，详情请参考笔记
     * @return
     */
    @Bean(name = "springSessionDefaultRedisSerializer")
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        //return new GenericJackson2JsonRedisSerializer(objectMapper());
        log.info("正在初始化 session-redis json化器");
        return new GenericFastJsonRedisSerializer();
    }

    /***
     * 配置 session-cookie
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();

        // 修改默认的session名字
        serializer.setCookieName("GULI_SESSION");
        serializer.setCookiePath("/");
        /**
         * 放大session的作用域
         * */
        serializer.setDomainName("guli.com");
        //serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        log.info("正在初始化 session-cookie");
        return serializer;
    }

    /**
     * 使用 jackson 进行 session 对象的序列化
     * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default
     * constructors
     *
     * @return the {@link ObjectMapper} to use
     */
    /*private ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
        return mapper;
    }*/

    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang
     * .ClassLoader)
     */
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.loader = classLoader;
    }

}